package com.ruoyi.web.controller.api;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.OrderDetail;
import com.ruoyi.system.domain.vo.MetricsQuery;
import com.ruoyi.system.domain.vo.OrderListQuery;
import com.ruoyi.system.mapper.FormSubMapper;
import com.ruoyi.system.mapper.OrderDetailMapper;
import com.ruoyi.web.controller.domain.EnterpriseInfo;
import com.ruoyi.web.controller.domain.RefundRequest;
import com.ruoyi.web.controller.service.EnterpriseInfoServiceImpl;
import com.ruoyi.web.controller.vo.EnterpriseInfoQuery;
import com.ruoyi.web.controller.vo.EnterpriseVO;
import com.ruoyi.web.controller.vo.OrderListVo;
import com.ruoyi.web.controller.vo.ShortLink;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 企业信息
 */
@Slf4j
@Anonymous
@Validated
@RestController
@RequestMapping("/enterprise")
public class EnterpriseInfoController extends BaseController {

    @Resource
    private EnterpriseInfoServiceImpl enterpriseInfoService;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private FormSubMapper formSubMapper;

    @GetMapping("/getLongLinkByShortLink/{shortLink}")
    public String getLongLinkByShortLink(@PathVariable("shortLink") String shortLink) {
        return enterpriseInfoService.getLongLinkByShortLink(shortLink);
    }

    // 生成短链
    @PostMapping("/getShortLink")
    public AjaxResult getShortLink(@RequestBody ShortLink link) {
        return AjaxResult.success(enterpriseInfoService.getShortLink(link));
    }

    // 修改长链信息
    @PostMapping("/updateLongLink")
    public AjaxResult updateLongLink(@RequestBody List<EnterpriseVO> vo) {
        return AjaxResult.success(enterpriseInfoService.updateLongLink(vo));
    }


    // 创建企业信息
    @PostMapping
    public AjaxResult createEnterpriseInfo(@Valid @RequestBody EnterpriseInfo enterpriseInfo) {
        return AjaxResult.success(enterpriseInfoService.createEnterpriseInfo(enterpriseInfo));
    }

    // 获取所有企业信息
    @PostMapping("/list")
    public TableDataInfo getAllEnterpriseInfo(@RequestBody EnterpriseInfoQuery query) {
        return enterpriseInfoService.getAllEnterpriseInfo(query);
    }

    // 获取所有企业信息
    @PostMapping("/listIsShortLinkNotEmpty")
    public TableDataInfo listIsShortLinkNotEmpty(@RequestBody EnterpriseInfoQuery query) {
        return enterpriseInfoService.listIsShortLinkNotEmpty(query);
    }

    // 根据ID获取企业信息
    @GetMapping("/{id}")
    public AjaxResult getEnterpriseInfoById(@PathVariable Long id) {
        return AjaxResult.success(enterpriseInfoService.getEnterpriseInfoById(id));
    }

    // 更新企业信息
    @PutMapping("/update")
    public AjaxResult updateEnterpriseInfo(@RequestBody EnterpriseInfo enterpriseInfo) {
        enterpriseInfoService.updateEnterpriseInfo(enterpriseInfo);
        return AjaxResult.success();
    }

    // 删除企业信息（逻辑删除）
    @DeleteMapping("/{id}")
    public AjaxResult deleteEnterpriseInfo(@PathVariable Long id) {
        enterpriseInfoService.deleteEnterpriseInfo(id);
        return AjaxResult.success();
    }

    // 批量删除
    @PostMapping("/batchDelByIds")
    public AjaxResult batchDelByIds(@RequestBody List<Long> ids) {
        enterpriseInfoService.batchDelByIds(ids);
        return AjaxResult.success();
    }

    // 批量下载企业信息
    @PostMapping("/download")
    public void downloadByIds(@RequestBody List<Long> ids, HttpServletResponse response) {
        enterpriseInfoService.downloadByIds(ids, response);
    }

    // 统计接口
    @PostMapping("/businessMetrics")
    public AjaxResult businessMetrics(@RequestBody MetricsQuery query) {
        return AjaxResult.success(enterpriseInfoService.businessMetrics(query));
    }


    // 订单查询
    @PostMapping("/orderList")
    public TableDataInfo getOrderList(@RequestBody OrderDetail orderDetail) {
        startPage();
        return getDataTable(orderDetailMapper.selectOrderDetailList(orderDetail));
    }


    // 订单查询
    @PostMapping("/orderListNew")
    public TableDataInfo orderListNew(@RequestBody OrderListQuery query) {
        startPage();
        return enterpriseInfoService.selectOrderDetailList(query);
    }

    //查询订单详情
    @GetMapping("/order/{id}")
    public AjaxResult getOrderDetail(@PathVariable Integer id) {
        OrderDetail orderDetail = orderDetailMapper.selectOrderDetailById(id);
        return success(orderDetail);
    }

    //编辑订单
    @PostMapping("/order/update")
    public AjaxResult orderUpdate(@RequestBody OrderDetail orderDetail) {
        if (Objects.isNull(formSubMapper.selectFormSubById(orderDetail.getIdForm()))) {
            return error("数据不存在");
        }
        return toAjax(orderDetailMapper.updateFrom(orderDetail));
    }

    //编辑订单
    @PostMapping("/order/updateNew")
    public AjaxResult orderUpdateNew(@RequestBody OrderListVo vo) {
        enterpriseInfoService.orderUpdateNew(vo);
        return success();
    }

    // 发起退款
    @PostMapping("/startRefund")
    public AjaxResult startRefund(Integer id, String refundReason) {
        enterpriseInfoService.startRefund(id, refundReason);
        return success();
    }


    // wx申请退款
    @PostMapping("/wxRefund")
    public AjaxResult refundOrder(@Valid @RequestBody RefundRequest refundRequest) {
        return success(enterpriseInfoService.refundOrder(refundRequest));
    }

    // 微信支付V3退款回调通知
    @PostMapping("/refund/notify")
    public R refundNotify(HttpServletRequest request) {
        try {
            // 处理退款回调通知
            enterpriseInfoService.handleRefundNotify(request);
            return R.ok();
        } catch (Exception e) {
            log.error("退款回调处理异常", e);
            return R.fail();
        }
    }

    // 支付宝申请退款
    @PostMapping("/alipayRefund")
    public AjaxResult alipayRefundOrder(@Valid @RequestBody RefundRequest refundRequest) {
        return success(enterpriseInfoService.alipayRefundOrder(refundRequest));
    }

}
